package io.litchi.auth.Infrastructure.handler;

import io.litchi.auth.Infrastructure.util.ResponseUtil;
import io.litchi.common.result.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;

/**
 * description: 登录失败的处理器
 *
 * @author helang
 * @since 2025-09-10
 */
public class LitchiLoginFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {

        Result<Object> failResult = Result.fail(exception.getMessage());
        ResponseUtil.writeJsonResponse(response, HttpStatus.UNAUTHORIZED.value(), failResult);
    }
}